package com.sunday.android.fangdaifu.bukong.adapter;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.InstallEquipment;
import com.sunday.android.fangdaifu.bukong.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EquipmentWithHeadersAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List<InstallEquipment> list = new ArrayList<>();

    public EquipmentWithHeadersAdapter(Context mContext,List<InstallEquipment> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.equip_view_list, parent, false);
        return new ItemHolder(view);
    }

    //单个内容
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ItemHolder itemHolder = (ItemHolder) holder;
        final InstallEquipment equipment = list.get(position);
        EquipInnerAdapter innerAdapter = new EquipInnerAdapter(mContext,equipment.getList());
        itemHolder.listView.setAdapter(innerAdapter);
        itemHolder.tvNews.setText(String.format("第%s层", BaseUtil.intToChiness(equipment.getFloor())));
        if (equipment.isHaveOpen()){
            itemHolder.more.setImageResource(R.mipmap.pull_up);
            itemHolder.listView.setVisibility(View.VISIBLE);
        } else {
            itemHolder.more.setImageResource(R.mipmap.pull_down);
            itemHolder.listView.setVisibility(View.GONE);
        }
        itemHolder.lPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (openListener!=null){
                    openListener.toggle(position,!equipment.isHaveOpen(),itemHolder.more);
                }
            }
        });
    }

    private OnOpenListener openListener;
    public interface OnOpenListener{
        void toggle(int position,boolean isOpen,View view);
    }
    public void setOpen(OnOpenListener openListener){
        this.openListener = openListener;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //item view
    class ItemHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.lPoint)
        View lPoint;
        @Bind(R.id.tvNews)
        TextView tvNews;
        @Bind(R.id.more)
        ImageView more;
        @Bind(R.id.list_view)
        ListView listView;
        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}